package com.dogstudio.touchblack.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;
import android.widget.Toast;

import com.dogstudio.touchblack.config.Config;
import com.dogstudio.touchblack.utils.AppUtils;
import com.dogstudio.touchblack.utils.LogUtils;


public class GamePanel extends View {
    private int colorBlack = Color.BLACK;
    private int colorWhite = Color.WHITE;
    private int colorDivider = Color.GRAY;
    private Paint mPaint;
    private Paint mLinePaint;
    private int blockWidth;
    private int blockHeight;
    private int dividerWidth;
    private int mGroup[][] = null;
    private int mCurRow = Config.ROW - 1;
    private Scroller mScroller;

    public GamePanel(Context context) {
        super(context);
        init();
    }

    public GamePanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mScroller = new Scroller(getContext());
        dividerWidth = 1;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setColor(colorDivider);
        mLinePaint.setStrokeWidth(dividerWidth);
        startGame();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = AppUtils.getScreenWidth(getContext());
        int height = AppUtils.getScreenHeight(getContext()) - AppUtils.getStatusHeight(getContext());
        blockWidth = width / Config.SIZE;
        blockHeight = (height - (Config.SIZE - 1) * dividerWidth) / Config.SIZE;
        LogUtils.e("width:" + width + ",height:" + height);
        LogUtils.e("blockWidth:" + blockWidth + ",blockHeight:" + blockHeight);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        setBackgroundColor(colorWhite);
        if (mCurRow == 0) {
            Toast.makeText(getContext(), "游戏成功", Toast.LENGTH_SHORT).show();
            startGame();
            return;
        }
        int max = Config.ROW - 1;
        int top = getHeight() - blockHeight;
        for (int i = max; i >= 0; i--) {
            int left = 0;
            for (int j = 0; j < Config.SIZE; j++) {
                if (mGroup[i][j] == Config.BLACK) {
                    // 绘制黑色块
                    mPaint.setColor(colorBlack);
                    canvas.drawRect(left, top, left + blockWidth, top + blockHeight, mPaint);
                }
                //绘制水平方向分割线
                left += blockWidth;
                canvas.drawLine(left, top, left, top + blockHeight, mLinePaint);
            }
            //绘制竖直分割线
            canvas.drawLine(0, top, getWidth(), top, mLinePaint);
            top -= dividerWidth;
            top -= blockHeight;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }
            float x = event.getX();
            float y = event.getY();
            int diff = mCurRow - 4;
            int i = (int) y / blockHeight;
            int j = (int) x / blockWidth;
            LogUtils.e("click item i=" + i + ", j=" + j + ",diff=" + diff + ",mCurRow=" + mCurRow);
            i += diff;
            if (i == mCurRow - 1 && mGroup[i][j] == Config.BLACK) {
                mScroller.startScroll(0, getScrollY(), 0, -(blockHeight + dividerWidth));
                invalidate();
                mCurRow--;
            } else {
                Toast.makeText(getContext(), "游戏失败", Toast.LENGTH_SHORT).show();
                startGame();
            }
        }
        return true;
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(0, mScroller.getCurrY());
            invalidate();
        }
    }

    public void startGame() {
        if (mGroup == null) {
            mGroup = new int[Config.ROW][Config.SIZE];
        }
        for (int i = 0; i < Config.ROW; i++) {
            for (int j = 0; j < Config.SIZE; j++) {
                mGroup[i][j] = Config.WHITE;
            }
            int random = AppUtils.getRandom();
            mGroup[i][random] = Config.BLACK;
        }
        mCurRow = Config.ROW;
    }

}
